package com.totcy.atom.model;

import android.content.Context;

import com.totcy.atom.bean.MMVideoResp;
import com.totcy.atom.net.HttpManager;
import com.totcy.atom.net.subscriber.BaseHttpSubscriber;
import com.totcy.atom.presenter.contract.MMVideoContract;
import com.totcy.baselibrary.base.mvpbase.BaseModelImpl;

public class MMVideoModel extends BaseModelImpl implements MMVideoContract.Model {

    private BaseHttpSubscriber<MMVideoResp> subscriber;

    public MMVideoModel(Context context) {
        super(context);
    }

    @Override
    public void getVideoList(int pageNum, DataCallback callback) {
        HttpManager.getApiService(mContext)
                .getMMVideo(pageNum)
                .compose(HttpManager.<MMVideoResp>applySchedulers())
                .compose(HttpManager.<MMVideoResp>handleResult())
                .subscribe(subscriber = new BaseHttpSubscriber<MMVideoResp>() {
                    @Override
                    public void onSuccess(MMVideoResp videoResp) {
                        callback.onVideoList(videoResp);
                    }

                    @Override
                    public void onError(int code, String errorMsg) {
                        callback.onFail(errorMsg);
                    }
                });
    }

    @Override
    public void canCelRequest() {
        if(subscriber != null){
            subscriber.unSubscribe();
        }
    }
}
